// 游戏设置页面逻辑

// 页面加载完成后执行
window.addEventListener('DOMContentLoaded', function() {
    // 加载保存的设置
    loadSettings();
    
    // 添加事件监听器
    document.getElementById('settingsForm').addEventListener('submit', saveSettings);
    document.getElementById('resetDefaultsBtn').addEventListener('click', resetDefaults);
    document.getElementById('gameDuration').addEventListener('input', updateDurationDisplay);
});

// 加载保存的设置
function loadSettings() {
    const settings = getGameSettings();
    
    // 设置滑块值
    document.getElementById('gameDuration').value = settings.gameDuration;
    
    // 更新显示值
    document.getElementById('durationValue').textContent = settings.gameDuration;
    
    // 设置难度选择
    document.getElementById('difficulty').value = settings.difficulty;
    
    // 设置音效开关
    document.getElementById('soundEnabled').checked = settings.soundEnabled;
}

// 保存设置
function saveSettings(e) {
    e.preventDefault();
    
    const settings = {
        gameDuration: parseInt(document.getElementById('gameDuration').value),
        difficulty: document.getElementById('difficulty').value,
        soundEnabled: document.getElementById('soundEnabled').checked
    };
    
    // 保存到本地存储
    if (saveGameSettings(settings)) {
        showMessage('设置已保存！', 'success');
    } else {
        showMessage('保存设置失败！', 'error');
    }
}

// 恢复默认设置
function resetDefaults() {
    const defaultSettings = {
        gameDuration: 60,
        difficulty: 'medium',
        soundEnabled: true
    };
    
    // 设置表单值
    document.getElementById('gameDuration').value = defaultSettings.gameDuration;
    document.getElementById('durationValue').textContent = defaultSettings.gameDuration;
    document.getElementById('difficulty').value = defaultSettings.difficulty;
    document.getElementById('soundEnabled').checked = defaultSettings.soundEnabled;
    
    // 保存默认设置
    if (saveGameSettings(defaultSettings)) {
        showMessage('已恢复默认设置！', 'success');
    }
}

// 更新时长显示
function updateDurationDisplay() {
    const duration = document.getElementById('gameDuration').value;
    document.getElementById('durationValue').textContent = duration;
}